import bpy
from ogreEx.blender.types import Operator
from ogreEx.context import get_header_, get__name__
from ogreEx.ui.UIClass import hide_user_interface, show_user_interface, OPERATOR

@OPERATOR
class OgreToggleInterfaceOp(Operator):
    '''Toggle Ogre UI'''
    bl_idname = "ogre.toggle_interface"
    bl_label = "Ogre UI"
    bl_options = {'REGISTER'}
    TOGGLE = True  #restore_minimal_interface()
    BLENDER_DEFAULT_HEADER = get_header_()
    
    @classmethod
    def poll(cls, context):
        return True

    def invoke(self, context, event):
        if OgreToggleInterfaceOp.TOGGLE: #get_header_():
            print( 'ogre.toggle_interface ENABLE' )
            #bpy.utils.register_module(get__name__())
            #set_header_(bpy.types.INFO_HT_header)
            show_user_interface()
            try: bpy.utils.unregister_class(get_header_())
            except: pass
            from ogreEx.ui.header.CINFO_HT_microheader import INFO_HT_microheader
            bpy.utils.unregister_class( INFO_HT_microheader )   # moved to custom header
            OgreToggleInterfaceOp.TOGGLE = False
        else:
            print( 'ogre.toggle_interface DISABLE' )
            #bpy.utils.unregister_module(__name__); # this is not safe, can segfault blender, why?
            hide_user_interface()
            bpy.utils.register_class(get_header_())
            from ogreEx.ui.restore_minimal_interface import restore_minimal_interface
            restore_minimal_interface()
            OgreToggleInterfaceOp.TOGGLE = True

        return {'FINISHED'}